package com.changgou.comment.controller;

import com.changgou.comment.pojo.Comment;
import com.changgou.comment.service.CommentService;
import com.changgou.comment.util.TokenDecode;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private TokenDecode tokenDecode;
    @Autowired
    private CommentService commentService;

    @PostMapping("/add")
    public Result add(@RequestBody Comment comment){
        //动态获取用户名
    String username = tokenDecode.getUserInfo().get("username");
        comment.setUsername(username);

        commentService.add(comment);
        return new Result();
}

    @GetMapping("/list")
    public Result<List<Comment>> list(@RequestParam("spuId")String spuId,@RequestParam("currentPage")Integer currentPage,@RequestParam("pageSize")Integer pageSize){
        List<Comment> list=commentService.findPage(spuId,currentPage,pageSize);
        return new Result<>(true, StatusCode.OK,"查询",list);
    }
}
